#!/usr/bin/perl -wc

package RFCMail::PackIt::Type::Keyed;

use strict;
use warnings;

use RFCMail::PackIt::Access;

# Object structure is [key_type, {key_value => remainder_type}]

sub define {
    my $class = shift;
    my $name = shift;
    my $key_type = shift;
    my $self = ref($_[0]) ? [$key_type, @_] : [$key_type, {@_}];
    bless $self, $class;
    register_type RFCMail::PackIt::Access $name, sub {$self->read(@_)}, sub {$self->write(@_)};
    return ($self);
}

sub read {
    my ($self, $access) = @_;
    my $key = $access->read_type($self->[0]);
    my $data = $access->read_type($self->[1]->{$key});
    return ([$key, $data]);
}

sub write {
    my $self = shift;
    my $access = shift;
    for (@_) {
	$access->write_type($self->[0], $_->[0]);
	$access->write_type($self->[1]->{$_->[0]}, $_->[1]);
    }
}

1;
